<!DOCTYPE html>
<html lang="zh">
	<head>
		<meta charset="UTF-8">
		<meta name="viewport" content="width=device-width, initial-scale=1.0">
		<title>便民设施</title>
		<style>
			* {
				padding: 0;
				margin: 0;
			}

			#app {
				padding: 10px;
			}

			header {
				display: none !important;
			}

			ul li {
				list-style: none;
			}

			a {
				text-decoration: none;
			}

			.mui-pull-caption {
				display: none;
			}

			.wx_service {
				padding: 10px 0;
				background: #fff;
				margin: 10px 0;
				text-align: center;
			}

			.wx_service .wx_ul ul {
				display: flex;
				justify-content: space-around;
				align-items: center;
			}

			.wx_service .wx_ul ul li {
				text-align: center;
				width: 50%;
				padding: 10px;
				box-sizing: border-box;
			}

			.wx_service .wx_ul ul li a {
				display: block;
				border-radius: 30px;
				padding: 10px;
				margin: 10px;
			}

			.wx_service .wx_ul ul li h2 {
				color: #666;
				font-size: 16px;
			}

			#he-plugin-standard {
				width: 100% !important;
			}

			.wv-lt-location a {
				display: none !important;
			}

			.wx_service .title {
				text-align: center;
				width: 60%;
				margin: 0 auto;
				position: relative;
				margin-bottom: 10x;
			}

			.wx_service .title:after {
				display: block;
				content: '';
				position: absolute;
				width: 100%;
				top: 50%;
				border: 1px solid #ccc;
			}

			.wx_service .title h2 {
				position: relative;
				font-size: 18px;
				color: #333;
				width: 76px;
				padding: 10px;
				background: #fff;
				margin: 0 auto;
				z-index: 2;
			}

			.box_div {
				padding: 10px;
				display: flex;
				flex-direction: column;
				justify-content: space-between;
				align-items: center;
				box-shadow: 0 0 12px #D6D6E3;
				border-radius: 10px;
			}

			.box_div img {
				width: 80%;
				height: 100px;
			}

			.box_div span {
				margin-top: 26px;
				font-size: 14px;
				color: #fff;
				padding: 7px 14px;
				background: #EA4E4E;
				border-radius: 4px;
			}

			.show_car {
				padding: 40px 0;
			}

			.show_car span {
				margin-top: 26px;
				font-size: 14px;
				color: #fff;
				padding: 7px 14px;
				background: #EA4E4E;
				border-radius: 4px;
			}
		</style>
	</head>
	<body>

		<div id="app" class="convenient">
			<!-- 天气插件 -->
			<div id="he-plugin-standard"></div>
			<div class="wx_service">
				<div class="title">
					<h2>便民设施</h2>
				</div>
				<div class="wx_ul">
					<ul>
						<li @click="wifi">
							<div class="box_div">
								<img src="../images/Router_img.png" alt="一键上网">
								<span>一键上网</span>
							</div>
						</li>
						<li @click="Enter_sm">
							<div class="box_div">
								<img src="../images/Paper_Machine.png" alt="一键取纸">
								<span>一键取纸</span>
							</div>
						</li>						
					</ul>
				</div>
				
			
			</div>
		</div>


		<script src="../js/mui.min.js"></script>
		<script src="../js/vue.min.js"></script>
		<script src="../js/base.js"></script>
		<!-- 获取中国天气插件！ -->

		<script src="https://widget.heweather.net/standard/static/js/he-standard-common.js?v=1.0"></script>
		<script>
			mui.init({
				pullRefresh: {
					container: ".convenient", //下拉刷新容器标识，querySelector能定位的css选择器均可，比如：id、.class等
					down: {
						style: 'circle', //必选，下拉刷新样式，目前支持原生5+ ‘circle’ 样式
						color: '#2BD009', //可选，默认“#2BD009” 下拉刷新控件颜色
						height: '50px', //可选,默认50px.下拉刷新控件的高度,
						range: '100px', //可选 默认100px,控件可下拉拖拽的范围
						offset: '0px', //可选 默认0px,下拉刷新控件的起始位置
						auto: false, //可选,默认false.首次加载自动上拉刷新一次
						callback: function() {
							window.location.reload();
						} //必选，刷新函数，根据具体业务来编写，比如通过ajax从服务器获取新数据；
					}
				}
			});
			//中国天气网创建
			WIDGET = {
				CONFIG: {
					"layout": 1,
					"width": "450",
					"height": "150",
					"background": 1,
					"dataColor": "FFFFFF",
					"borderRadius": 5,
					"city": "CN101090301",
					"key": "07c64e99aa3e41718618a2ce14c34025"
				}
			}
			mui.plusReady(function() {
				var vm = new Vue({
					el: '#app',
					data: {
						isWifi: 0, //是否开启wifi
					},

					methods: {
						Enter_sm() { //进入扫二维码页面
							BASE.Custom_window('barcode/takepaper.html', 'takepaper');
						},
						set() {
							//一键上网
							 androidWifiConnection('testwifi', '12345678', 'wpa');
							// androidWifiConnection('HongDeXin-PRO', 'a12345678', 'wpa');

							// androidWifiConnection('testwifi','nopass');


							/**
							 * 安卓自动连接wifi，根据制定的ssid
							 * @param {Object} ssid 名称
							 * @param {Object} password 密码
							 * @param {Object} type 加密方式
							 */
							function androidWifiConnection(ssid, password, type) {

								var MainActivity = plus.android.runtimeMainActivity();
								var Context = plus.android.importClass('android.content.Context');
								plus.android.importClass("android.net.wifi.WifiManager");
								plus.android.importClass("android.net.wifi.WifiInfo");
								plus.android.importClass("android.net.wifi.WifiConfiguration");
								var wifiManager = MainActivity.getSystemService(Context.WIFI_SERVICE);

								//WifiConfiguration
								var wifiConfig = androidCreateWifiInfo(ssid, password, type);
								//
								if (wifiConfig == null) {
									console.log("wifiConfig is null!")
									return;
								}

								//WifiConfiguration
								var WifiConfiguration = tempConfig = isExsitsAndroid(ssid);

								if (tempConfig != null) {
									console.log("删除原来连接的wifi" + tempConfig);
									wifiManager.removeNetwork(tempConfig.plusGetAttribute('networkId'));
								}
								//int
								console.log("要连接的新的wifi配置：" + wifiConfig)
								var netID = wifiManager.addNetwork(wifiConfig);
								console.log(netID);
								//boolean
								var enabled = wifiManager.enableNetwork(netID, true);
								console.log("enableNetwork status enable=" + enabled)
								// boolean
								var connected = wifiManager.reconnect();
								console.log("enableNetwork connected=" + connected)
								
								
							}




							/**
							 * 查看以前是否也配置过这个网络
							 */
							function isExsitsAndroid(sSID) {
								console.log("查看以前是否也配置过这个网络" + sSID);
								//WifiConfiguration
								var MainActivity = plus.android.runtimeMainActivity();
								var Context = plus.android.importClass('android.content.Context');
								plus.android.importClass("android.net.wifi.WifiManager");
								plus.android.importClass("android.net.wifi.WifiInfo");
								var List = plus.android.importClass("java.util.List");
								var ArrayList = plus.android.importClass("java.util.ArrayList");
								var wifiManager = MainActivity.getSystemService(Context.WIFI_SERVICE);
								//
								var existingConfigs = new ArrayList();
								existingConfigs = wifiManager.getConfiguredNetworks();
								if (existingConfigs.size() != 0) {
									for (var i = 0; i < existingConfigs.size(); i++) {
										if (existingConfigs.get(i).plusGetAttribute('SSID') == ("\"" + sSID + "\"")) {
											console.log("该制定的ssid存在于配置中:" + sSID);
											return existingConfigs.get(i);
										}
									}
								}
								console.log("该ssid没有配置过")
								return null;
							}

							/**
							 * 创建wifi配置对象
							 * @param {Object} SSID
							 * @param {Object} Password
							 * @param {Object} Type
							 */
							function androidCreateWifiInfo(SSID, Password, Type) {
								plus.android.importClass("java.util.BitSet");
								var WifiConfiguration = plus.android.importClass("android.net.wifi.WifiConfiguration");
								var config = new WifiConfiguration();
								config.plusGetAttribute('allowedAuthAlgorithms').clear();
								config.plusGetAttribute('allowedGroupCiphers').clear();
								config.plusGetAttribute('allowedKeyManagement').clear();
								config.plusGetAttribute('allowedPairwiseCiphers').clear();
								config.plusGetAttribute('allowedProtocols').clear();
								config.plusSetAttribute('SSID', "\"" + SSID + "\"");
								// nopass
								if (Type == "nopass") {
									//  var allowedKeyManagement = plus.android.getAttribute(config, 'allowedKeyManagement');  
									// plus.android.importClass(allowedKeyManagement);  
									// allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);  
									config.plusSetAttribute(wepKeys[0], "");
									config.plusGetAttribute('allowedKeyManagement').set(WifiConfiguration.KeyMgmt.NONE);
									config.plusSetAttribute('wepTxKeyIndex', 0);
								}
								// wep
								if (Type == "wep") {
									if (!Password != "") {
										if (isHexWepKey(Password)) {
											config.plusSetAttribute(wepKeys[0], Password);
										} else {
											config.plusSetAttribute(wepKeys[0], "\"" + Password + "\"");
										}
									}
									config.allowedAuthAlgorithms.set(AuthAlgorithm.OPEN);
									config.allowedAuthAlgorithms.set(AuthAlgorithm.SHARED);
									config.allowedKeyManagement.set(KeyMgmt.NONE);
									config.plusSetAttribute('wepTxKeyIndex', 0);
								}
								// wpa
								if (Type == "wpa") {
									config.plusSetAttribute('preSharedKey', "\"" + Password + "\"");
									config.plusSetAttribute('hiddenSSID', true);

									config.plusGetAttribute('allowedAuthAlgorithms').set(WifiConfiguration.AuthAlgorithm.OPEN);
									config.plusGetAttribute('allowedGroupCiphers').set(WifiConfiguration.GroupCipher.TKIP);
									config.plusGetAttribute('allowedKeyManagement').set(WifiConfiguration.KeyMgmt.WPA_PSK);
									config.plusGetAttribute('allowedPairwiseCiphers').set(WifiConfiguration.PairwiseCipher.TKIP);
									// 此处需要修改否则不能自动重联
									// config.plusGetAttribute('allowedProtocols').set(WifiConfiguration.Protocol.WPA);
									config.plusGetAttribute('allowedGroupCiphers').set(WifiConfiguration.GroupCipher.CCMP);
									config.plusGetAttribute('allowedPairwiseCiphers').set(WifiConfiguration.PairwiseCipher.CCMP);
									config.plusSetAttribute('status', WifiConfiguration.Status.ENABLED);
								}
								return config;
							}


						},
						openWIFI() {
							// 开启WIFI
							var _this=this;
							function androidOpenWifi() {
								var bRet = false;
								var MainActivity = plus.android.runtimeMainActivity()
								var Context = plus.android.importClass('android.content.Context')
								plus.android.importClass("android.net.wifi.WifiManager")
								plus.android.importClass("android.net.wifi.WifiInfo")
								var wifiManager = MainActivity.getSystemService(Context.WIFI_SERVICE)
								if (!wifiManager.isWifiEnabled()) {
									bRet = wifiManager.setWifiEnabled(true); //返回自动打开的结果

									if (bRet) {
										mui.toast("WIFI已开启");
										_this.isWifi = 1;										
									} else {
										mui.toast("WIFI未开启,请打开WIFI");
										_this.isWifi = 0;
									}
									// console.log("打开wifi的返回结果是" + bRet)
									
								} else {
									bRet = true;
									mui.toast("WIFI已开启");
									_this.isWifi = 1;									
								}
								return bRet;
							}
							androidOpenWifi();
						},
						Enter_shop() {
							//进入购物界面
							BASE.Custom_window('shop/shop_list.html', 'shop_list');
						},
						wifi(){
							plus.webview.open('wifi/wifi.html', 'wifi/wifi.html', {}, 'slide-in-right', 200);
						}
					},
					watch: {
						isWifi: {
							//高级监听
							handler(newVal,oldVal) {
								if(newVal==1){
									var _this=this;
									// 这里故意让延迟5秒加载,如果直接执行会报错~~因为获取不到 wifi 列表
									setTimeout(function() {
										_this.set();
									}, 5000);
										
								}
							},
							deep: true,
							immediate: true,
							
						},
					},
					created() {
						
					},

				})
				
				
			})
			
			//定位张家口天气
		</script>

	</body>
</html>
